package com.by.web.controller;

import com.by.common.core.bean.ResultBean;
import com.by.common.core.exception.BusinessException;
import com.by.common.core.response.R;
import com.by.common.validate.ValidationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice//异常增强
@Slf4j
public class GlobalExceptinController {
    /**
     * 如果当前异常类中  没有指定处理具体的异常 走这里....
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResultBean handlerException(Exception e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return R.fail("服务器开小差了", e.getMessage());
    }

    @ExceptionHandler(ValidationException.class)
    public ResultBean handlerValicationException(ValidationException e) {
        //注意 如果不打印异常栈信息  控制台就不会显示
        log.error(e.getMessage());
        log.error("验证失败:"+e.getErrorField());
        return R.fail("验证失败", e.getErrorField());
    }

    @ExceptionHandler(BusinessException.class)
    public ResultBean handlerBusinessException(BusinessException e){
        return R.fail(e.getMessage());
    }
}
